package cn.tedu.micro.demo.client.dlx;

import cn.tedu.micro.demo.client.RabbitChannelManager;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 先声明组件
 */
public class DlxDeclare {
    public static final String BIZ_EX="biz_ex";
    public static final String BIZ_Q="biz_q";
    public static final String DLX_EX="dlx_ex";
    public static final String DLX_Q="dlx_q";
    public static final String DLX_RK="dlx_rk";
    //使用上述的变量值 声明死信架构
    @Test
    public void declare()throws Exception{
        Channel channel = RabbitChannelManager.newChannel();
        //1.声明业务交换机 fanout
        channel.exchangeDeclare(BIZ_EX, BuiltinExchangeType.FANOUT);
        //2.声明死信交换机 direct
        channel.exchangeDeclare(DLX_EX, BuiltinExchangeType.DIRECT);
        //3.声明死信队列
        channel.queueDeclare(DLX_Q,false,false,false,null);
        //4.声明业务队列 绑定死信交换机 死信路由
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange",DLX_EX);
        args.put("x-dead-letter-routing-key",DLX_RK);
        channel.queueDeclare(BIZ_Q,false,false,false,args);
        //5.业务交换机和业务队列绑定
        channel.queueBind(BIZ_Q,BIZ_EX,"");
        //6.死信交换机和死信队列绑定
        channel.queueBind(DLX_Q,DLX_EX,DLX_RK);
    }
}
